package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.StudentGradeContinue;
import com.xmy.cultivate.mapper.StudentGradeContinueMapper;
import com.xmy.cultivate.service.IStudentGradeContinueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 学生续报 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-06-19
 */
@Service
public class StudentGradeContinueServiceImpl extends ServiceImpl<StudentGradeContinueMapper, StudentGradeContinue> implements IStudentGradeContinueService {

    @Override
    public void addData(StudentGrade studentGrade, Integer isPresort) {
        try {
            if (studentGrade.getLessonType().equals(1)){
                LocalDate dateNow = LocalDate.now();
                QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
                wrapper.eq("year_part", studentGrade.getYearPart());
                wrapper.eq("quarter_num", studentGrade.getQuarterNum());
                wrapper.eq("student_id", studentGrade.getStudentId());
                wrapper.eq("subjects_id", studentGrade.getSubjectsId());
                wrapper.eq("lesson_type", studentGrade.getLessonType());
                wrapper.le("start_date",dateNow);
                wrapper.ge("end_date",dateNow);
                Long count = this.count(wrapper);
                if (count == 0L){
                    StudentGradeContinue studentGradeContinue = new StudentGradeContinue();
                    studentGradeContinue.setYearPart(studentGrade.getYearPart());
                    studentGradeContinue.setQuarterNum(studentGrade.getQuarterNum());
                    studentGradeContinue.setSchoolId(studentGrade.getSchoolId());
                    studentGradeContinue.setGradeId(studentGrade.getGradeId());
                    studentGradeContinue.setCourseId(studentGrade.getCourseId());
                    studentGradeContinue.setStudentId(studentGrade.getStudentId());
                    studentGradeContinue.setLessonType(studentGrade.getLessonType());
                    studentGradeContinue.setSubjectsId(studentGrade.getSubjectsId());
                    studentGradeContinue.setYearClassId(studentGrade.getYearClassId());
                    studentGradeContinue.setIsPresort(isPresort);
                    studentGradeContinue.setStartDate(dateNow);
                    String dateStr = "2099-12-31";
                    // 直接解析 ISO 格式的日期字符串
                    LocalDate endDate = LocalDate.parse(dateStr);
                    studentGradeContinue.setEndDate(endDate);
                    this.save(studentGradeContinue);
                }else {
                    List<StudentGradeContinue> list = this.list(wrapper);
                    for (StudentGradeContinue studentGradeContinue : list){
                        //如果失败，调整为1
                        if (studentGradeContinue.getIsPresort().equals(4)){
                            studentGradeContinue.setIsPresort(isPresort);
                            this.updateById(studentGradeContinue);
                        }
                    }
                }
            }
        }catch (Exception e){}

    }

    @Override
    public void changeEndDate(StudentGrade studentGrade){
        try {
            LocalDate dateNow = LocalDate.now();
            QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
            wrapper.eq("year_part", studentGrade.getYearPart());
            wrapper.eq("quarter_num", studentGrade.getQuarterNum());
            wrapper.eq("student_id", studentGrade.getStudentId());
            wrapper.eq("subjects_id", studentGrade.getSubjectsId());
            wrapper.eq("lesson_type", studentGrade.getLessonType());
            wrapper.le("start_date",dateNow);
            wrapper.ge("end_date",dateNow);
            List<StudentGradeContinue> list = this.list(wrapper);
            for (StudentGradeContinue studentGradeContinue : list){
                LocalDate afterDate = dateNow.minusDays(1);
                if (studentGradeContinue.getStartDate().isAfter(afterDate)){
                    this.removeById(studentGradeContinue);
                }else {
                    studentGradeContinue.setEndDate(afterDate);
                    this.updateById(studentGradeContinue);
                }
            }
        }catch (Exception e){}
    }

    @Override
    public void changeFail(StudentGrade studentGrade, Integer isPresort){
        try {
            LocalDate dateNow = LocalDate.now();
            QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
            wrapper.eq("year_part", studentGrade.getYearPart());
            wrapper.eq("quarter_num", studentGrade.getQuarterNum());
            wrapper.eq("student_id", studentGrade.getStudentId());
            wrapper.eq("subjects_id", studentGrade.getSubjectsId());
            wrapper.eq("lesson_type", studentGrade.getLessonType());
            wrapper.le("start_date",dateNow);
            wrapper.ge("end_date",dateNow);
            List<StudentGradeContinue> list = this.list(wrapper);
            for (StudentGradeContinue studentGradeContinue : list){
                LocalDate afterDate = dateNow.minusDays(1);
                if (studentGradeContinue.getStartDate().isAfter(afterDate)){
                    this.removeById(studentGradeContinue);
                }else {
                    studentGradeContinue.setIsPresort(isPresort);
                    studentGradeContinue.setEndDate(afterDate);
                    this.updateById(studentGradeContinue);
                }
            }
        }catch (Exception e){}
    }

    @Override
    public void changeIsPresort(StudentGrade studentGrade, Integer isPresort){
        try {
            LocalDate dateNow = LocalDate.now();
            QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
            wrapper.eq("year_part", studentGrade.getYearPart());
            wrapper.eq("quarter_num", studentGrade.getQuarterNum());
            wrapper.eq("student_id", studentGrade.getStudentId());
            wrapper.eq("subjects_id", studentGrade.getSubjectsId());
            wrapper.eq("lesson_type", studentGrade.getLessonType());
            wrapper.le("start_date",dateNow);
            wrapper.ge("end_date",dateNow);
            List<StudentGradeContinue> list = this.list(wrapper);
            for (StudentGradeContinue studentGradeContinue : list){
                studentGradeContinue.setIsPresort(isPresort);
                this.updateById(studentGradeContinue);
            }
            //如果不存在，就添加
            if (list.size() == 0){
                this.addData(studentGrade,isPresort);
            }
        }catch (Exception e){}
    }
}
